
*******************************************************************************
****						Table 5: Panel C Autocorrelation ****
*******************************************************************************
use ".\data\clean\I10W_laneuse_dataset_15nov14_wcensus", clear

merge m:1 date hour using ".\data\clean\HV_ML_reliab.dta", keep(1 3) nogen 
gen reliabilityML=dist/p20_speedML-dist/p50_speedML
gen reliabilityHV=dist/p20_speedHV-dist/p50_speedHV
gen reliability_diff=reliabilityML-reliabilityHV

*WTP calculation
gen TT_dif_hr=dist/MLspeed-dist/ELspeed
gen WTP2=charged_toll/TT_dif_hr
drop if ELspeed==.
drop if TT_dif_hr==.
drop if holiday==1
la var TT_dif_hr "Time in Hours"
drop if dow==0|dow==6
keep if acct_type=="PRIVATE"&occupancy~="HOV-3"
keep if hour>4 & hour<9
replace reliability_diff=0 if reliability_diff<0.01
keep if reliability_diff~=.
la var reliability_diff "Reliability"
g week=week(date)

g en_num=substr(entry_plaza,-1,1)
g ex_num=substr(exit_plaza,-1,1)
destring en_num, replace
destring ex_num, replace

/*
** segment dummies for spatial HAC matrix
g _s1=0
replace _s1=1 if en_num==1
g _s2=0
replace _s2=1 if en_num<3
g _s3 =0
replace _s3=1 if en_num<4 & ex_num>3
g _s4=0
replace _s4=1 if en_num<5 & ex_num>4
g _s5=0
replace _s5=1 if ex_num==6
*/



preserve

g t = floor(entry_time/(60*5*1000))
collapse charged_toll TT_dif_hr reliability_diff (first) entry_pm exit_pm ///
	if TT_dif_hr>0,by(rt_id t)
sort rt_id
egen id=group(rt_id)
qui sum id
local idmax=`r(max)'

forval ii=1/`idmax'{ // loop over routes
	qui sum entry_pm if id==`ii'
	local ep=`r(mean)'
	qui sum exit_pm if id==`ii'
	local xp=`r(mean)'
	g _d`ii'=0
	replace _d`ii'=-min(`xp'-entry_pm, exit_pm-`ep') if `xp'< entry_pm // =0 mileage of overlap
	g _r`ii'=_d`ii'>0 // =1 if overlap >0
	}
tsset id t
est sto clear
acreg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, ///
	id(id) time(t) lag(1) network links_mat(_r*) dist(1) hac
est sto a1
acreg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, ///
	id(id) time(t) lag(2) network links_mat(_r*) dist(1) hac
est sto a2
acreg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, ///
	id(id) time(t) lag(3) network links_mat(_r*) dist(1) hac
est sto a3
acreg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, ///
	id(id) time(t) lag(1) network dist_mat(_d*) dist(1) hac
est sto a4
acreg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, ///
	id(id) time(t) lag(2) network dist_mat(_d*) dist(1) hac
est sto a5
acreg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, ///
	id(id) time(t) lag(3) network dist_mat(_d*) dist(1) hac
est sto a6
acreg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, ///
	id(id) time(t) lag(1) network dist_mat(_d*) dist(2) hac
est sto a7

esttab  a1 a2 a3 a4 a5 a6 a7  using ".\results\revision1\FEs_D.csv", replace  ///
	cells(b(star fmt(%9.2f)) se(par)) star(* 0.10 ** 0.05 *** 0.01) ///
		stats(r2 N ll aic bic,fmt(%9.2f %9.0f) ) label ///
	order(_cons TT_dif_hr reliability_diff) keep( TT_dif_hr _cons reliability_diff) ///
	mtitle(I II III IV V VI VII) 
restore

g lat=.
g longitude =.
replace lat =34.072275 if entry_plaza=="WT01"
replace lat =34.071733 if entry_plaza=="WT02"
replace lat =34.071529 if entry_plaza=="WT03"
replace lat =34.062659 if entry_plaza=="WT04"
replace lat =34.061256 if entry_plaza=="WT05"
replace lat =34.030759 if entry_plaza=="WT06"
replace longitude =-118.055886 if entry_plaza=="WT01"
replace longitude =-118.100352 if entry_plaza=="WT02"
replace longitude =-118.117242 if entry_plaza=="WT03"
replace longitude =-118.161275 if entry_plaza=="WT04"
replace longitude =-118.167056 if entry_plaza=="WT05"
replace longitude =-118.222988 if entry_plaza=="WT06"

est sto clear
acreg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, id(id) time(t) lag(1) ///
	 spatial latitude(lat) longitude(longitude) dist(1)
est sto a1

acreg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, id(id) time(t) lag(1) ///
	 spatial latitude(lat) longitude(longitude) dist(1) hac
est sto a2

acreg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, id(id) time(t) lag(1) ///
	 spatial latitude(lat) longitude(longitude) dist(2) hac
est sto a3

acreg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, id(id) time(t) lag(2) ///
	 spatial latitude(lat) longitude(longitude) dist(1) hac
est sto a4

esttab  a1 a2 a3 a4   using ".\results\revision1\conley.csv", replace  ///
	cells(b(star fmt(%9.2f)) se(par)) star(* 0.10 ** 0.05 *** 0.01) ///
		stats(r2 N ll aic bic,fmt(%9.2f %9.0f) ) label ///
	order(_cons TT_dif_hr reliability_diff) keep( TT_dif_hr _cons reliability_diff) ///
	mtitle(I II III IV) 

preserve
*Newey-West Errors to Account for last appearance(s)
est sto clear
egen id=group(acct_no)
sort acct_no date hour
egen t=seq(),by(acct_no)
tsset id t
reg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0,  cluster(rt_id)
est sto a0
newey charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, lag(1) force 
est sto b0
newey charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, lag(2) force 
est sto b1
newey charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, lag(3)  force 
est sto b2
newey charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, lag(4) force 
est sto b3
esttab  a0 b0 b1 b2 b3  using ".\results\revision1\FEs_B.csv", replace  ///
	cells(b(star fmt(%9.2f)) se(par)) star(* 0.10 ** 0.05 *** 0.01) ///
		stats(r2 N ll aic bic,fmt(%9.2f %9.0f) ) label ///
	order(_cons TT_dif_hr reliability_diff) keep( TT_dif_hr _cons reliability_diff) ///
	mtitle(I II III IV V) 

restore


preserve
*Newey-West Errors to Account for previous toll
est sto clear
g t = floor(entry_time/(60*5*1000))
reg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0,  cluster(rt_id)
est sto a0
collapse charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0,by(rt_id t)
tsset rt_id t
newey charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, lag(1) force 
est sto c0
newey charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, lag(2) force 
est sto c1
newey charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, lag(3) force 
est sto c2
newey charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, lag(4) force 
est sto c3
esttab  a0 c0 c1 c2 c3  using ".\results\revision1\FEs_C.csv", replace  ///
	cells(b(star fmt(%9.2f)) se(par)) star(* 0.10 ** 0.05 *** 0.01) ///
		stats(r2 N ll aic bic,fmt(%9.2f %9.0f) ) label ///
	order(_cons TT_dif_hr reliability_diff) keep( TT_dif_hr _cons reliability_diff) ///
	mtitle(I II III IV V) 

restore


est sto clear
reg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0,  cluster(rt_id)
est sto a0
reg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0, robust
est sto a1
reg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0,  cluster(hour)
est sto a2
reg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0,  cluster(date)
est sto a2
reg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0,  cluster(week)
est sto a3
reg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0,  cluster(month)
est sto a4
reg charged_toll TT_dif_hr reliability_diff if TT_dif_hr>0,  cluster(acct_no)
est sto a5
cgmreg charged_toll TT_dif_hr reliability_diff  if TT_dif_hr>0,  cluster(rt_id week)
est sto a6
cgmreg charged_toll TT_dif_hr reliability_diff  if TT_dif_hr>0,  cluster(rt_id hour)
est sto a7
cgmreg charged_toll TT_dif_hr reliability_diff  if TT_dif_hr>0,  cluster(date hour)
est sto a8

esttab  a0 a1 a2 a3 a4 a5 a6 a7 a8 using ".\results\revision1\tabs\FEs_A.csv", replace  ///
	cells(b(star fmt(%9.2f)) se(par)) star(* 0.10 ** 0.05 *** 0.01) ///
		stats(r2 N ll aic bic,fmt(%9.2f %9.0f) ) label ///
	order(_cons TT_dif_hr reliability_diff) keep( TT_dif_hr _cons reliability_diff) ///
	mtitle(I II III IV V VI VII VIII) 
